﻿using AutoMapper;
using Practice.DTO;
using Practice.PH4_2104A.IRepository;
using Practice.PH4_2104A.IService;
using Practice.PH4_2104A.Shopping.SevenLayerFramework.Domin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice.Service
{
    public class OrderMasterService : IOrderMasterService
    {
        IOrderMasterRepository _masterRepository;
        IOrderDetailRepository _detailRepository;
        ISKURepository _skuRepository;
        IUnitOfWork _unitOfWork;
        IMapper _map;

        public OrderMasterService(IOrderMasterRepository masterRepository, IOrderDetailRepository detailRepository, ISKURepository skuRepository, IUnitOfWork unitOfWork, IMapper map)
        {
            _masterRepository = masterRepository;
            _detailRepository = detailRepository;
            _skuRepository = skuRepository;
            _unitOfWork = unitOfWork;
            _map = map;
        }

        public int Delete(OrderMasterModel model)
        {
            throw new NotImplementedException();
        }

        public int Delete(List<OrderMasterModel> model)
        {
            throw new NotImplementedException();
        }

        //查询所有
        public IQueryable<OrderMasterModel> GetAll(int pageIndex, int pageSize, out int total)
        {
            var data = _masterRepository.GetAll();
            total = data.Count();
            return data;
        }

        //查询一条订单及对应的商品信息
        public dynamic GetFirstByOrderNo(string orderNo)
        {
            var q = from a in _masterRepository.GetAll()
                    where a.OrderNo.Equals(orderNo)
                    select new
                    {
                        master=a,
                        products = _detailRepository.GetAll().Where(x=>x.OrderNo.Equals(a.OrderNo)).ToList()
                    };
            return q;
        }


        //创建订单
        public int Insert(OrderInputDTO dto)
        {
            using (var tran = _unitOfWork.GetDbContext().Database.BeginTransaction())
            {
                try
                {
                    //1、将数据保存到订单主表
                    var master = _map.Map<OrderMasterInputDTO, OrderMasterModel>(dto.Master);
                    _masterRepository.Insert(master);

                    //2、将数据保存到订单明细表
                    var details = _map.Map<List<OrderDetailInputDTO>, List<OrderDetailModel>>(dto.Details);
                    details.ForEach(x => x.OrderNo = master.OrderNo);
                    _detailRepository.Insert(details);

                    //3、修改库存

                    _unitOfWork.SaveChanges(); //将内存中的改变，提交到数据库 
                    tran.Commit();//提交事务
                }
                catch (Exception)
                {
                    tran.Rollback(); //回滚事务
                    throw;
                }
            }
            return 0;
        }

        public int Update(OrderMasterModel model)
        {
            throw new NotImplementedException();
        }

        public int Update(List<OrderMasterModel> model)
        {
            throw new NotImplementedException();
        }

        //统计订单的商品销量
        public dynamic GetOrderDetail()
        {
            var data = _detailRepository.GetAll().Select(x => new { x.SkuId, x.Name, x.Number });


            var q = from a in data.GroupBy(x => x.Name)
                    select new { 
                        Name=a.Key,
                        Value = a.Sum(y=>y.Number)
                    };

            var data1 = q.Select(x => x.Name);
            var data2 = q.Select(x => x.Value);

            return new { data = q, data1,data2};
        }
    }
}
